package leetcode.code0223;

public class Solution extends Solution223 {

	@Override
	public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
		int areaA = (ax2 - ax1) * (ay2 - ay1);
		if (ax1 == bx1 && ay1 == by1 && ax2 == bx2 && ay2 == by2) {
			return areaA;
		}
		int areaB = (bx2 - bx1) * (by2 - by1);
		int areaAB = areaA + areaB;
		int y3 = this.isAinB(ay1, ay2, by1, by2);
		int x3 = this.isAinB(ax1, ax2, bx1, bx2);
		if (x3 > 0 && y3 > 0) {
			return areaAB - y3 * x3;
		} else {
			return areaAB;
		}
	}

	private int isAinB(int ay1, int ay2, int by1, int by2) {
		int y1 = Math.max(ay1, by1);
		int y2 = Math.min(ay2, by2);
		return y2 - y1;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();

	}

}
